package data;


import java.util.ArrayList;
import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;

public class Folio extends AbstractData {
	private transient long id = -1;
	private long hotelId;
	private long displayId;
	private List<Charge> charge;
	private String chargeString;
	private List<Payment> payment;
	private String paymentString;
	private boolean isLock;
	
	public Folio(){}
	
	public Folio(long id)
	{
		this.id = id;
	
	}
	
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getDisplayId() {
		return displayId;
	}
	public void setDisplayId(long displayId) {
		this.displayId = displayId;
	}
	public long getHotelId() {
		return hotelId;
	}

	public void setHotelId(long hotelId) {
		this.hotelId = hotelId;
	}

	public List<Charge> getCharge() {
		if ((charge == null || charge.size() == 0) && !(chargeString == null || chargeString.isEmpty())) {
			String[] idOfList = SplitList(chargeString);
			List<Charge> list = new ArrayList<Charge>();
			for (String id : idOfList) {
				Charge c = new Charge(Long.parseLong(id));
				list.add(c);
			}
			
			for(Charge c : list)
				c.LoadData();
			
			charge = list;
		}
		return charge;
	}

	public void setCharge(List<Charge> charge) {
		this.chargeString = CombineList(charge);
		this.charge = charge;
	}

	public List<Payment> getPayment() {
		if ((payment == null || payment.size() == 0) && !(paymentString == null || paymentString.isEmpty())) {
			String[] idOfList = SplitList(paymentString);
			List<Payment> list = new ArrayList<Payment>();
			for (String id : idOfList) {
				Payment c = new Payment(Long.parseLong(id));
				list.add(c);
			}
			
			for(Payment c : list)
				c.LoadData();
			
			payment = list;
		}
		return payment;
	}

	public void setPayment(List<Payment> payment) {
		this.paymentString = CombineList(payment);
		this.payment = payment;
	}

	public void setChargeString(String chargeString) {
		this.chargeString = chargeString;
	}

	public void setPaymentString(String paymentString) {
		this.paymentString = paymentString;
	}

	public boolean isLock() {
		return isLock;
	}

	public void setLock(boolean isLock) {
		this.isLock = isLock;
	}

	public String getChargeString() {
		return chargeString;
	}

	public String getPaymentString() {
		return paymentString;
	}

	@Override
	public Entity getEntity() {
		Entity item;
		if(this.key == null)
			item = new Entity(EntityName.Folio);
		else
			item = new Entity(this.key);

		item.setProperty("displayId", this.displayId);
		item.setProperty("HotelId", this.hotelId);
		item.setProperty("payment", this.paymentString);
		item.setProperty("charge", this.chargeString);
		item.setProperty("isLock", this.isLock);
		
		return item;
	}

	@Override
	public void setEntity(Entity entity) {
		this.id = entity.getKey().getId();
		this.displayId = (Long) entity.getProperty("displayId");
		this.hotelId = (Long) entity.getProperty("HotelId");
		this.paymentString = (String) entity.getProperty("payment");
		this.chargeString = (String) entity.getProperty("charge");
		this.isLock = (Boolean) entity.getProperty("isLock");

		this.key = entity.getKey();
	}

	public void LoadData() {
		if (this.id > -1) {
			Entity entity = LoadData(EntityName.Folio, this.id);
			setEntity(entity);
		}
	}
}
